package com.yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;
import com.yunhe.bean.CommodityInfo;
import com.yunhe.bean.page;
import com.yunhe.inter.CommodityInfoInter;
import com.yunhe.util.SqlSessionFactoryUtil;
/**
 * 
 * @author qwl
 * 主要处理首页面的商品列表信息。
 *
 */

@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	SqlSession sqlSession;
       
   
    public IndexServlet() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * 建立数据库连接
		 */
		/*request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//获取sqlSession
		sqlSession = SqlSessionFactoryUtil.openSqlSession();
		//获取mapper
		CommodityInfoInter commMapper =  sqlSession.getMapper(CommodityInfoInter.class);
		//创建商品所属小类
		Map<String, Object> csc=new HashMap<String, Object>();
		if(request.getParameter("small_id")==null && request.getParameter("small_id")==""){
			response.sendRedirect("index.jsp");
		}
		csc.put("small_id",Integer.parseInt(request.getParameter("small_id")));
		//获取商品总数并分页
		int total=commMapper.totalCommodity();
		page p=new page();
		p.setPageNum(16);
		p.setCurrentPage(1);
		if(request.getParameter("CurrentPage")!=null && request.getParameter("CurrentPage")!=""){
			p.setCurrentPage(Integer.parseInt(request.getParameter("CurrentPage")));
			}
		p.setTotalNews(total);
		csc.put("page", p);
		//得到分类商品列表 
		ArrayList<CommodityInfo> list  = commMapper.showCommodityInfo(csc);
		//放入request中
		request.setAttribute("list", list);
		//转到index.jsp
		request.getRequestDispatcher("/product-list.jsp").forward(request, response);*/
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//获取sqlSession
		sqlSession = SqlSessionFactoryUtil.openSqlSession();
		//获取mapper
		CommodityInfoInter commMapper =  sqlSession.getMapper(CommodityInfoInter.class);
		//建立输出流
		PrintWriter out=response.getWriter();
		//建立Map集合
		Map<String, Object> map=new HashMap<String, Object>();
		//获取商品总数并分页
		int total=commMapper.totalCommodity();
		page p=new page();
		p.setPageNum(8);
		p.setCurrentPage(1);
		if(request.getParameter("CurrentPage")!=null && request.getParameter("CurrentPage")!=""){
			p.setCurrentPage(Integer.parseInt(request.getParameter("CurrentPage")));
		}
		p.setTotalNews(total);
		//得到 CommodityInfo 
		ArrayList<CommodityInfo> list  = commMapper.showCommodityInfoo(p);
		sqlSession.close();
		//map集合填入数据
		map.put("list", list);
		map.put("page", p);
		//转换Json格式
		JSONObject job=JSONObject.fromObject(map);
		System.out.println(job.toString());
		out.write(job.toString());
	}

}
